/**
  ******************************************************************************
  * @file    py32f420_ll_epwm.c
  * @author  MCU Application Team
  * @brief   EPWM LL module driver.
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; Copyright (c) 2023 Puya Semiconductor Co.
  * All rights reserved.</center></h2>
  *
  * This software component is licensed by Puya under BSD 3-Clause license,
  * the "License"; You may not use this file except in compliance with the
  * License. You may obtain a copy of the License at:
  *                        opensource.org/licenses/BSD-3-Clause
  *
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; Copyright (c) 2016 STMicroelectronics.
  * All rights reserved.</center></h2>
  *
  * This software component is licensed by ST under BSD 3-Clause license,
  * the "License"; You may not use this file except in compliance with the
  * License. You may obtain a copy of the License at:
  *                        opensource.org/licenses/BSD-3-Clause
  *
  ******************************************************************************
  */
#if defined(USE_FULL_LL_DRIVER)

/* Includes ------------------------------------------------------------------*/
#include "py32f420_ll_epwm.h"
#include "py32f420_hal_rcc.h"

#ifdef  USE_FULL_ASSERT
#include "py32_assert.h"
#else
#define assert_param(expr) ((void)0U)
#endif /* USE_FULL_ASSERT */

/** @addtogroup PY32F420_LL_Driver
  * @{
  */

#if defined (EPWM1) || defined (EPWM2) || defined (EPWM3) || defined (EPWM4) || defined (EPWM5) || defined (EPWM6)

/** @addtogroup EPWM_LL
  * @{
  */

/* Private types -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* Private constants ---------------------------------------------------------*/
/* Private macros ------------------------------------------------------------*/
/** @addtogroup EPWM_LL_Private_Macros
  * @{
  */

#define IS_LL_EPWM_PRESCALER(__VALUE__) (((__VALUE__) == LL_EPWM_CLOCK_DIVIDER_1) \
                                            || ((__VALUE__) == LL_EPWM_CLOCK_DIVIDER_2) \
                                            || ((__VALUE__) == LL_EPWM_CLOCK_DIVIDER_4) \
                                            || ((__VALUE__) == LL_EPWM_CLOCK_DIVIDER_8) \
                                            || ((__VALUE__) == LL_EPWM_CLOCK_DIVIDER_16) \
                                            || ((__VALUE__) == LL_EPWM_CLOCK_DIVIDER_32) \
                                            || ((__VALUE__) == LL_EPWM_CLOCK_DIVIDER_64) \
                                            || ((__VALUE__) == LL_EPWM_CLOCK_DIVIDER_128))


#define IS_LL_EPWM_HIGHSPEEDPRESCALER(__VALUE__) (((__VALUE__) == LL_EPWM_HSCLOCK_DIVIDER_1) \
                                                 || ((__VALUE__) == LL_EPWM_HSCLOCK_DIVIDER_2) \
                                                 || ((__VALUE__) == LL_EPWM_HSCLOCK_DIVIDER_4) \
                                                 || ((__VALUE__) == LL_EPWM_HSCLOCK_DIVIDER_6) \
                                                 || ((__VALUE__) == LL_EPWM_HSCLOCK_DIVIDER_8) \
                                                 || ((__VALUE__) == LL_EPWM_HSCLOCK_DIVIDER_10) \
                                                 || ((__VALUE__) == LL_EPWM_HSCLOCK_DIVIDER_12) \
                                                 || ((__VALUE__) == LL_EPWM_HSCLOCK_DIVIDER_14))

/**
  * @}
  */



/* Exported functions --------------------------------------------------------*/
/** @addtogroup EPWM_LL_Exported_Functions
  * @{
  */

/** @addtogroup EPWM_LL_EF_Init
  * @{
  */

/**
  * @brief  Set EPWMx registers to their reset values.
  * @param  EPWMx EPWM instance
  * @retval An ErrorStatus enumeration value:
  *          - SUCCESS: EPWMx registers are de-initialized
  *          - ERROR: invalid EPWMx instance
  */
ErrorStatus LL_EPWM_DeInit(const EPWM_TypeDef *EPWMx)
{
  ErrorStatus result = SUCCESS;

  /* Check the parameters */
  assert_param(IS_EPWM_INSTANCE(EPWMx));

  if ((EPWMx == EPWM1) || (EPWMx == EPWM2) || (EPWMx == EPWM3) || (EPWMx == EPWM4) || (EPWMx == EPWM5) || (EPWMx == EPWM6))
  {
    __HAL_RCC_EPWM_FORCE_RESET();
    __HAL_RCC_EPWM_RELEASE_RESET();
  }
  else
  {
    result = ERROR;
  }

  return result;
}


/**
  * @brief  Set the fields of the EPWMe base unit configuration data structure
  *         to their default values.
  * @param  EPWM_InitStruct pointer to a @ref LL_EPWM_InitTypeDef structure (EPWMe base unit configuration data structure)
  * @retval None
  */
void LL_EPWM_StructInit(LL_EPWM_InitTypeDef *EPWM_InitStruct)
{
  /* Set the default configuration */
  EPWM_InitStruct->Prescaler           = LL_EPWM_CLOCK_DIVIDER_1;
  EPWM_InitStruct->HighSpeedPrescaler  = LL_EPWM_HSCLOCK_DIVIDER_1;
  EPWM_InitStruct->Autoreload          = 0xFFFFU;
}

/**
  * @brief  Configure the EPWMx EPWMe base unit.
  * @param  EPWMx EPWMer Instance
  * @param  EPWM_InitStruct pointer to a @ref LL_EPWM_InitTypeDef structure
  *         (EPWMx EPWMe base unit configuration data structure)
  * @retval An ErrorStatus enumeration value:
  *          - SUCCESS: EPWMx registers are de-initialized
  *          - ERROR: not applicable
  */
ErrorStatus LL_EPWM_Init(EPWM_TypeDef *EPWMx, const LL_EPWM_InitTypeDef *EPWM_InitStruct)
{
  /* Check the parameters */
  assert_param(IS_EPWM_INSTANCE(EPWMx));
  assert_param(IS_LL_EPWM_PRESCALER(EPWM_InitStruct->Prescaler));
  assert_param(IS_LL_EPWM_HIGHSPEEDPRESCALER(EPWM_InitStruct->HighSpeedPrescaler));

  LL_EPWM_EALLOW(EPWMC);

  MODIFY_REG(EPWMx->TBCTL,EPWM_TBCTL_CLKDIV | EPWM_TBCTL_HSPCLKDIV,EPWM_InitStruct->Prescaler | EPWM_InitStruct->HighSpeedPrescaler);

  WRITE_REG(EPWMx->TBPRD,EPWM_InitStruct->Autoreload);

  return SUCCESS;
}

/**
  * @}
  */

/**
  * @}
  */

/**
  * @}
  */
#endif 

/**
  * @}
  */

#endif /* USE_FULL_LL_DRIVER */

